package com.xlh.controller.cs;

import com.xlh.common.Result;
import com.xlh.dto.cs.CourseListDTO;
import com.xlh.dto.cs.CourseSystemDTO;
import com.xlh.dto.cs.CourseSystemTreeDTO;
import com.xlh.enums.cs.EditItemEnum;
import com.xlh.enums.cs.OperationTypeEnum;
import com.xlh.param.cs.CourseSystemParam;
import com.xlh.param.cs.EditCourseSystemParam;
import com.xlh.security.UserInfoHolder;
import com.xlh.service.cs.CourseSystemCourseService;
import com.xlh.service.cs.CourseSystemLearningPhaseService;
import com.xlh.service.cs.CourseSystemService;
import com.xlh.service.cs.CourseSystemWorkPlaceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zhr 2019/10/15
 */
@Api(description = "课程体系", value = "CourseSystemController")
@RestController
@RequestMapping("/course-system")
public class CourseSystemController {

    @Autowired
    private CourseSystemService courseSystemService;
    @Autowired
    private CourseSystemCourseService courseService;
    @Autowired
    private CourseSystemLearningPhaseService learningPhaseService;
    @Autowired
    private CourseSystemWorkPlaceService workPlaceService;
    @Autowired
    private UserInfoHolder userInfoHolder;

    @GetMapping("")
    @ApiOperation("展示课程体系树状图")
    public Result<List<CourseSystemTreeDTO>> showCourseSystem(@RequestParam(required = false) Long workId) {
        return Result.success(courseSystemService.showCourseSystem(workId, userInfoHolder.getUserId()));
    }

    @GetMapping("/course")
    @ApiOperation("编辑课程体系页面数据展示")
    public Result<CourseListDTO> listCourse() {
        return Result.success(courseSystemService.listCourse());
    }

    @PostMapping("/course")
    @ApiOperation("保存课程体系分配情况")
    public Result editCourseSystem(@RequestBody EditCourseSystemParam param){
        courseSystemService.editCourseSystem(param);
        return Result.success();
    }

    @GetMapping("/not-system-course")
    @ApiOperation("展示非系统课程")
    public Result showCustomCourse() {
        return Result.success(courseService.showNotSystemCourse());
    }

    @PostMapping("/not-system-course")
    @ApiOperation("编辑非系统课程")
    public Result editCustomCourse(@RequestBody List<CourseSystemParam> paramList) {
        for (CourseSystemParam param : paramList) {
            if (param.getId() == null) {
                param.setOperation(OperationTypeEnum.INSERT.getType());
            }
        }

        courseSystemService.editCourseSystem(paramList, EditItemEnum.COURSE);

        return Result.success();
    }

    @GetMapping("/learning-phase")
    @ApiOperation("展示学习阶段")
    public Result<List<CourseSystemDTO>> showLearningPhase() {
        return Result.success(learningPhaseService.listLearningPhase());
    }

    @PostMapping("/learning-phase")
    @ApiOperation("编辑学习阶段")
    public Result editLearningPhase(@RequestBody List<CourseSystemParam> paramList) {
        for (CourseSystemParam param : paramList) {
            if (param.getId() == null) {
                param.setOperation(OperationTypeEnum.INSERT.getType());
            }
        }

        courseSystemService.editCourseSystem(paramList, EditItemEnum.LEARNING_PHASE);

        return Result.success();
    }

    @GetMapping("/work-place")
    @ApiOperation("展示工作岗位")
    public Result showWorkPlace() {
        return Result.success(workPlaceService.listWorkPlace());
    }

    @PostMapping("/work-place")
    @ApiOperation("编辑工作岗位")
    public Result editWorkPlace(@RequestBody List<CourseSystemParam> paramList) {
        for (CourseSystemParam param : paramList) {
            if (param.getId() == null) {
                param.setOperation(OperationTypeEnum.INSERT.getType());
            }
        }

        courseSystemService.editCourseSystem(paramList, EditItemEnum.WORK_PLACE);

        return Result.success();
    }

}
